#include"headfile.h"


int inquire(int num)
{
    if(admin_num == num) //管理员查询操作
    {
        return _inquireAdmin();
    }
    else if(stu_num == num) //普通用户查询操作
    {
        return _inquireStu(); //_inquireStu()返回0 则表示返回到学生用户选择操作界面menu(3)
    }
}

int _inquireStu(void)
{
    char inquire[10];
s:  printf("请输入您要查询什么 信息/返回:");
    memset(inquire,0,10);
    scanf("%s",inquire);
    if(0 == strcmp(inquire,"信息")) //inquire输入的不是“返回”，就从文件中匹配相关信息
    {
        FILE * fp = fopen("./document/student.txt","r");
        if(NULL == fp)
        {
            printf("打开文件失败:%m\n");
            return -1;
        }

        member student = {0,0,0,0,0};
        while(4 == fscanf(fp,"%s%s%d%d",student.name,student.sex,&(student.age),&(student.id)))
        {
            char id[N] = {0};
            _itoa(student.id,id,10); //将整形转换为数组，当stu输入的是id号，以方便和输入的stu进行比较
            //stu和从文件中读取的姓名或者id号进行比对
            if(0 == strcmp(id_login, id))
            {
                printf("姓名:%s 性别:%s 年龄:%d id号:%d\n",student.name,student.sex,student.age,student.id);
                break;
            }
        }

        fclose(fp);

        goto s; //查询完输入的内容可以通过goto跳转到s继续查询
    }
    else if(0 == strcmp(inquire,"返回")) return 0; //inquire输入的是返回，则返回0
    else
    {
        printf("你输入的操作有误,");
        goto s;
    }
}

int _inquireAdmin(void)
{
    char inquire[10];
a:  printf("请输入要查询的对象 指定学生(id号/姓名)/全部/返回:");
    memset(inquire,0,7);
    scanf("%s%*c",inquire);
    if(0 != strcmp(inquire,"返回")) //inquire输入的不是“返回”
    {
        FILE * fp = fopen("./document/student.txt","r");
        if(NULL == fp)
        {
            printf("打开文件失败:%m\n");
            return -1;
        }

        if(0 == strcmp(inquire,"全部")) //inquire输入的是“全部”
        {
            int num = getLine("./document/student.txt"); //获取文件中的行数，一行一学生，即所有学生的个数
            memNode * p = NULL;
            for(int i = 0; i < num; i++) //将文件中存储的学生的信息放到链表中
            {
                member student;
                fscanf(fp,"%s%s%d%d",student.name,student.sex,&(student.age),&(student.id));
                tailAdd(&p,getNode(student,stu_num)); //尾插法存信息
            }
            printf("已存在的学生有%d个:\n",num);
            traverse(p);
        }
        else //inquire输入的是指定学生(id号/姓名)
        {
            member student = {0,0,0,0,0};
            bool flag = false;
            while(4 == fscanf(fp,"%s%s%d%d",student.name,student.sex,&(student.age),&(student.id)))
            {
                char id[10] = {0};
                _itoa(student.id,id,10); //将整形转换为数组，当stu输入的是id号，以方便和输入的stu进行比较
                //stu和从文件中读取的姓名或者id号进行比对
                if(0 == strcmp(inquire,student.name) || 0 == strcmp(inquire,id))
                {
                    flag = true;
                    printf("姓名:%s 性别:%s 年龄:%d id号:%d\n",student.name,student.sex,student.age,student.id);
                }
            }
            if(false == flag)
            {
                printf("您要查询的用户不在文件中!\n");
            }
        }

        fclose(fp);
        goto a;  //查询完输入的内容可以通过goto跳转到u继续查询
    }
    else if(0 == strcmp(inquire,"返回")) return 0; //inquire输入的是返回,则返回0
    else
    {
        printf("你输入的操作有误,");
        goto a;
    }
}
